package MapHomeWork.fifth;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class StudentBusiness {
    private static Set<Student> studentSet = new HashSet<>();
    static {
        studentSet.add(new Student(1,"蒋金鑫",24));
        studentSet.add(new Student(2,"赵元",24));
        studentSet.add(new Student(3,"赵稼楠",24));
        studentSet.add(new Student(4,"胡敏",24));
        studentSet.add(new Student(5,"蔡家莹",24));
    }

    public static void addStudent(Student student){
        if (studentSet.contains(student)){
            System.out.println("该学生已经存在了，不能再添加了");
        }else {
            studentSet.add(student);
        }
    }

    public static void deleteStudent(int sId){
        boolean falg=false;
        for (Student s: studentSet) {
            if (s.getsId()==sId){
                studentSet.remove(s);
                falg=true;
            }
        }
        if (falg){
            System.out.println("删除成功");
        }else {
            System.out.println("不存在这个学生");
        }
    }

    public static void updateStudent(Student student){

        Iterator<Student> it=studentSet.iterator();
        boolean falg=false;
        while (it.hasNext()){
            Student s=it.next();
            if (s.getsId()==student.getsId()){
                it.remove();
                studentSet.add(student);
                falg=true;
            }
        }
        if (falg){
            System.out.println("删除成功");
        }else {
            System.out.println("不存在这个学生");
        }
    }
    public static Set<Student> findByName(String name){
        Set<Student> set = new HashSet<>();
        for (Student s: studentSet) {
            if (s.getsName().equals(name)){
                set.add(s);
            }
        }
        return set;
    }

    public static void exit(){
        System.out.println("退出系统");
    }
}
